/*
Copyright 2010  Murali Devi (profdevi@gmail.com)

This file is part of ComineGL Project.

ComineGL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ComineGL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ComineGL.  If not, see <http://www.gnu.org/licenses/>.

This code uses the irrlicht and irrklang libraries.  Please read
the libraries copyrights at 

	irrlicht library:  http://irrlicht.sourceforge.net
	irrklang library:  http://www.ambiera.com/irrklang

*/



//v0.14 copyright Comine.com 20101212U01
#include <stdarg.h>
#include "MStdLib.h"
#include "Global.h"
#include "ComineGLCamera.h"


//***********************************************
//** Exported Functions
//***********************************************
MDLLFUN bool CGLCameraSetActive(int nodeid)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}
	
	// Search for camera node
	MCGLNodeInfo *nodeinfo=GlobalNodeList.Find(nodeid);
	if(nodeinfo==NULL)
		{
		MStdPrintf("**Unable to find camera node %d\n",nodeid);
		return false;
		}

	if(nodeinfo->GetNodeType()!=CGLNIT_CAMERA
			&& nodeinfo->GetNodeType()!=CGLNIT_CAMERA_MAYA
			&& nodeinfo->GetNodeType()!=CGLNIT_CAMERA_FPS )
		{
		MStdPrintf("**Node %d is not a camera type\n",nodeid);
		return false;
		}

	GlobalRefSceneMgr->setActiveCamera((ICameraSceneNode *)nodeinfo->GetNode());
	return true;
	}


//////////////////////////////////////////////////////////
MDLLFUN bool CGLCameraLookAt(int nodeid,float x,float y,float z)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}
	

	// Find Node
	MCGLNodeInfo *nodeinfo=GlobalNodeList.Find(nodeid);
	if(nodeinfo==NULL)
		{
		MStdPrintf("**Unable to find node with id=%d\n",nodeid);
		return false;
		}

	if(nodeinfo->GetNodeType()!=CGLNIT_CAMERA
			&& nodeinfo->GetNodeType()!=CGLNIT_CAMERA_MAYA
			&& nodeinfo->GetNodeType()!=CGLNIT_CAMERA_FPS )
		{
		MStdPrintf("No Camera Found with id %d\n",nodeid);
		return true;
		}

	ICameraSceneNode *cameranode=(ICameraSceneNode *)nodeinfo->GetNode();
	cameranode->setTarget(vector3d<float>(x,y,z) );

	return true;
	}


//////////////////////////////////////////////////////////
MDLLFUN bool CGLCameraFPSKeyClearAll(void)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	if(GlobalCameraControl.Clear()==false)
		{
		MStdPrintf("**Unable to clear fps camera key mappings\n");
		return false;
		}
	
	return true;
	}


//////////////////////////////////////////////////
MDLLFUN bool CGLCameraFPSKeyForward(int keycode)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	// If Key code is bad, then clear the key mapping
	if(keycode<=0)
		{
		if(GlobalCameraControl.Remove(EKA_MOVE_FORWARD)==false)
			{
			MStdPrintf("**Unable to remove camera fps key mapping\n");
			return false;
			}

		return true;
		}

	if(GlobalCameraControl.Add(EKA_MOVE_FORWARD,(EKEY_CODE)keycode)==false)
		{
		MStdPrintf("**Unable to add key mapping\n");
		return false;
		}
	
	return true;
	}


/////////////////////////////////////////////////
MDLLFUN bool CGLCameraFPSKeyBackward(int keycode)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	// If Key code is bad, then clear the key mapping
	if(keycode<=0)
		{
		if(GlobalCameraControl.Remove(EKA_MOVE_BACKWARD)==false)
			{
			MStdPrintf("**Unable to remove camera fps key mapping\n");
			return false;
			}

		return true;
		}

	if(GlobalCameraControl.Add(EKA_MOVE_BACKWARD,(EKEY_CODE)keycode)==false)
		{
		MStdPrintf("**Unable to add key mapping\n");
		return false;
		}
	
	return true;
	}


///////////////////////////////////////////////////////
MDLLFUN bool CGLCameraFPSKeyLeft(int keycode)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	// If Key code is bad, then clear the key mapping
	if(keycode<=0)
		{
		if(GlobalCameraControl.Remove(EKA_STRAFE_LEFT)==false)
			{
			MStdPrintf("**Unable to remove camera fps key mapping\n");
			return false;
			}

		return true;
		}

	if(GlobalCameraControl.Add(EKA_STRAFE_LEFT,(EKEY_CODE)keycode)==false)
		{
		MStdPrintf("**Unable to add key mapping\n");
		return false;
		}
	
	return true;
	}


////////////////////////////////////////////////////
MDLLFUN bool CGLCameraFPSKeyRight(int keycode)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	// If Key code is bad, then clear the key mapping
	if(keycode<=0)
		{
		if(GlobalCameraControl.Remove(EKA_STRAFE_RIGHT)==false)
			{
			MStdPrintf("**Unable to remove camera fps key mapping\n");
			return false;
			}

		return true;
		}

	if(GlobalCameraControl.Add(EKA_STRAFE_RIGHT,(EKEY_CODE)keycode)==false)
		{
		MStdPrintf("**Unable to add key mapping\n");
		return false;
		}
	
	return true;
	}


